﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Configuration;
using System.Net;
using System.Text;

namespace wsvisitas
{
    /// <summary>
    /// Descripción breve de authService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // Para permitir que se llame a este servicio web desde un script, usando ASP.NET AJAX, quite la marca de comentario de la línea siguiente. 
    [System.Web.Script.Services.ScriptService]
    public class authService : System.Web.Services.WebService
    {
        public string ObtieneCadena() {

            return ConfigurationManager.ConnectionStrings["CadenaConexion"].ConnectionString;
        }

        private string obtieneIP()
        {
            IPHostEntry host;
            string IP = "";
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily.ToString() == "InterNetwork")
                {
                    IP = ip.ToString() + "-" + IP;
                }
            }

            return IP;
        }

        [WebMethod]
        public int servLogin(string usuario, string pass) {            
            int id=0;
            byte[] b = Convert.FromBase64String(pass);
            string value = ASCIIEncoding.ASCII.GetString(b);
            SqlConnection conn = new SqlConnection(ObtieneCadena());
            SqlCommand cmd = new SqlCommand("sp_T_MAES_USUARIO_Login", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@vchUSUARIO", usuario));
            cmd.Parameters.Add(new SqlParameter("@vchPASS",value));
            try
            {
                cmd.Connection.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                    id = dr.GetInt32(3);
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }catch(Exception ex){
                cmd.Dispose();
                conn.Dispose();
            }
            return id;
        }

        [WebMethod]
        public List<String> servListarUrls() {
            List<String> lst = new List<string>();
            SqlConnection conn = new SqlConnection(ObtieneCadena());
            SqlCommand cmd = new SqlCommand("sp_T_MAES_URL_Listar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                cmd.Connection.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                    lst.Add(dr.GetString(0));
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex) {
                cmd.Dispose();
                conn.Dispose();
            }
            return lst;
        }

        [WebMethod]
        public DataSet servListarUrls2()
        {
            List<String> lst = new List<string>();
            SqlConnection conn = new SqlConnection(ObtieneCadena());
            SqlCommand cmd = new SqlCommand("sp_T_MAES_URL_Listar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            DataSet ds = new DataSet();
            try
            {
                cmd.Connection.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
               
                da.Fill(ds);

                da.Dispose();
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch (Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
            }
            return ds;
        }

        [WebMethod]
        public void servLog(string usario,string Url,string ip) {
            SqlConnection conn = new SqlConnection(ObtieneCadena());
            SqlCommand cmd = new SqlCommand("sp_T_MAES_LOG_Insertar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            try {                
                cmd.Parameters.Add(new SqlParameter("@vchUSUARIO", usario));
                cmd.Parameters.Add(new SqlParameter("@vchURL", Url));
                cmd.Parameters.Add(new SqlParameter("@vchIPINGRESO", ip));
                cmd.Parameters.Add(new SqlParameter("@vchIPPUBLICO", obtieneIP()));
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch (Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
            }
        }

        [WebMethod]
        public string[][] listaFtp() {
            List<string[]> lst = new List<string[]>();
            SqlConnection conn = new SqlConnection(ObtieneCadena());
            SqlCommand cmd = new SqlCommand("sp_T_MAES_FTP_Listar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            
            try
            {
                cmd.Connection.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read()) {
                    string[] str = new string[4];
                    str[0] = dr.GetString(1);
                    str[1] = dr.GetString(2);
                    str[2] = dr.GetString(3);
                    str[3] = dr.GetString(4);
                    lst.Add(str);
                }
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch {
                cmd.Dispose();
                conn.Dispose();
            }
            return lst.ToArray();
        }

        [WebMethod]
        public List<string> listarUsuarios() {
            List<string> lst = new List<string>();
            SqlConnection conn = new SqlConnection(ObtieneCadena());
            SqlCommand cmd = new SqlCommand("sp_T_MAES_USUARIO_Listar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                cmd.Connection.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                    lst.Add(dr.GetString(0));
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch (Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
            }
            return lst;
        }
        
        [WebMethod]
        public string[][] listarLogs(string usuario) {
            List<string[]> lst = new List<string[]>();
            SqlConnection conn = new SqlConnection(ObtieneCadena());
            SqlCommand cmd = new SqlCommand("sp_T_MAES_LOG_Listar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vchUSUARIO", usuario));
                cmd.Connection.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    string[] str = new string[6];
                    str[0] = dr.GetString(0);
                    str[1] = dr.GetString(1);
                    str[2] = dr.GetString(2);
                    str[3] = dr.GetInt32(4).ToString();
                    str[4] = dr.GetDateTime(3).ToShortDateString();
                    str[5] = dr.GetDateTime(3).ToShortTimeString();
                    lst.Add(str);
                }
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch {
                cmd.Dispose();
                conn.Dispose();
            }
            return lst.ToArray();
        }         
    }
}
